home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / fntool / utils.c < prev    next >
Text File  |  1993-12-06  |  3KB  |  179 lines

  1. #include "fntool.h"
  2. #include <stdarg.h>
  3.  
  4. void fatalerr(char *msg,...)
  5. {
  6.     va_list ap;
  7.  
  8.     va_start(ap,msg);
  9.     fprintf(stderr,"\nFATAL ERROR: ");
  10.     vfprintf(stderr,msg,ap);
  11.     putc('\n',stderr);
  12.     va_end(ap);
  13.     if(infile != NULL)
  14.         fclose(infile);
  15.     if(outfile != NULL) {
  16.         fclose(outfile);
  17.         unlink(outname);
  18.     }
  19.     exit(1);
  20. }
  21.  
  22. void *safemalloc(int size)
  23. {
  24.     void *result = malloc(size);
  25.  
  26.     if(result == NULL) fatalerr("out of memory");
  27.     memset(result,0,size);
  28.     return(result);
  29. }
  30.  
  31. char **makebytemap(int w,int h)
  32. {
  33.     int  ii = (w * h * sizeof(char)) + (h * sizeof(char *));
  34.     char *bitmap,**result = (char **)safemalloc(ii);
  35.  
  36.     bitmap = (char *)(&result[h]);
  37.     for(ii = 0; ii < h; ii++,bitmap += w) result[ii] = bitmap;
  38.     return(result);
  39. }
  40.  
  41. void copybytemap(char **dp,int dx,int dy,char **sp,int sx,int sy,int w,int h)
  42. {
  43.     while(--h >= 0) memcpy(&dp[dy+h][dx],&sp[sy+h][sx],w);
  44. }
  45.  
  46. void setbytemap(char **bp,int value,int x,int y,int w,int h)
  47. {
  48.     while(--h >= 0) memset(&bp[y+h][x],value,w);
  49. }
  50.  
  51. int rowbit(char **bmp,int row)
  52. {
  53.     int ii;
  54.  
  55.     for(ii = fnt.height; --ii >= 0; )
  56.         if(bmp[row][ii] != 0) return(1);
  57.     return(0);
  58. }
  59.  
  60. int colbit(char **bmp,int col)
  61. {
  62.     int ii;
  63.  
  64.     for(ii = fnt.maxwidth; --ii >= 0; )
  65.         if(bmp[ii][col] != 0) return(1);
  66.     return(0);
  67. }
  68.  
  69. chr *getchr(int code)
  70. {
  71.     chr *cp;
  72.  
  73.     for(cp = fnt.chars; cp != NULL; cp = cp->next)
  74.         if(cp->code == code) return(cp);
  75.     return(NULL);
  76. }
  77.  
  78. void closeinput(void)
  79. {
  80.     if(infile != NULL) {
  81.         fclose(infile);
  82.         infile = NULL;
  83.         inname[0] = '\0';
  84.     }
  85. }
  86.  
  87. void closeoutput(void)
  88. {
  89.     if(outfile != NULL) {
  90.         int waserror = ferror(outfile);
  91.         fclose(outfile);
  92.         outfile = NULL;
  93.         if(waserror) {
  94.         unlink(outname);
  95.         fatalerr("error writing output file \"%s\"",outname);
  96.         }
  97.         outname[0] = '\0';
  98.     }
  99. }
  100.  
  101. void openinput(char *name,char *mode)
  102. {
  103.     if(infile != NULL)
  104.         closeinput();
  105.     if((infile = fopen(name,mode)) == NULL)
  106.         fatalerr("can not open input file \"%s\"",name);
  107.     strcpy(inname,name);
  108. }
  109.  
  110. void openoutput(char *name,char *mode)
  111. {
  112.     if(outfile != NULL)
  113.         closeoutput();
  114.     if((outfile = fopen(name,mode)) == NULL)
  115.         fatalerr("can not create output file \"%s\"",name);
  116.     strcpy(outname,name);
  117. }
  118.  
  119. char *readline(void)
  120. {
  121.     static char buff[300];
  122.  
  123.     if(fgets(buff,299,infile) == NULL)
  124.         fatalerr("unexpected end of input file \"%s\"",inname);
  125.     return(buff);
  126. }
  127.  
  128. void computewidth(void)
  129. {
  130.     chr  *cp;
  131.     long totalwdt = 0;
  132.  
  133.     fnt.minwidth = 32000;
  134.     fnt.maxwidth = -32000;
  135.     for(cp = fnt.chars; cp != NULL; cp = cp->next) {
  136.         if(fnt.maxwidth < cp->width) fnt.maxwidth = cp->width;
  137.         if(fnt.minwidth > cp->width) fnt.minwidth = cp->width;
  138.         totalwdt += cp->width;
  139.     }
  140.     fnt.avgwidth = totalwdt / (fnt.maxchar - fnt.minchar + 1);
  141. }
  142.  
  143. int strmatch(char *arg,char *pat)
  144. {
  145.     while(*pat != '\0') {
  146.         if(tolower(*arg) != tolower(*pat)) return(0);
  147.         arg++;
  148.         pat++;
  149.     }
  150.     return(1);
  151. }
  152.  
  153. void splitfamily(void)
  154. {
  155.     char *p;
  156.  
  157.     if((p = strrchr(fnt.family,'_')) != NULL) {
  158.         if(strmatch(p,"_ital")) {
  159.         strcpy(fnt.slant,"ital");
  160.         *p = '\0';
  161.         }
  162.     }
  163.     if((p = strrchr(fnt.family,'_')) != NULL) {
  164.         if(strmatch(p,"_bold")) {
  165.         strcpy(fnt.weight,"bold");
  166.         *p = '\0';
  167.         }
  168.         if(strmatch(p,"_thin")) {
  169.         strcpy(fnt.weight,"thin");
  170.         *p = '\0';
  171.         }
  172.         if(strmatch(p,"_demi")) {
  173.         strcpy(fnt.weight,"demi");
  174.         *p = '\0';
  175.         }
  176.     }
  177. }
  178.  
  179.